package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description：
 * @version:
 * @author：ruixinag
 * @create：Created in 2021/11/8 20:08
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;
    //物资来源分页列表展示
    @Override
    public PageInfo<SupplierVO> findSupplierList(Map map) {
        String pageNum = (String) map.get("pageNum");
        String pageSize = (String) map.get("pageSize");
        String name= (String) map.get("name");
        String address = (String) map.get("address");
        String contact = (String) map.get("contact");

        Example o = new Example(Supplier.class);
        Example.Criteria criteria = o.createCriteria();

        if(!StringUtils.isEmpty(name)){
            criteria.andLike("name", "%"+name + "%");
        }
        if(!StringUtils.isEmpty(address)){
            criteria.andLike("address", "%"+address + "%");
        }

        if(!StringUtils.isEmpty(contact)){
            criteria.andEqualTo("contact",contact);
        }
        PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        List<Supplier> suppliers = supplierMapper.selectByExample(o);
        PageInfo pageInfo = new PageInfo<>(suppliers);
        List<SupplierVO> list = suppliers.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());

        pageInfo.setList(list);

        return pageInfo;
    }

    //添加物资来源
    @Override
    public void add(Supplier supplier) {
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplierMapper.insert(supplier);
    }

    //添加物资来源
    @Override
    public SupplierVO edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);
        return supplierVO;
    }

    //更新数据
    @Override
    public void update(Supplier supplier) {
        supplier.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKey(supplier);
    }

    //删除数据
    @Override
    public void delete(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }

    //获取全部
    @Override
    public List<SupplierVO> findAll() {

        List<Supplier> supplierList = supplierMapper.selectAll();
        List<SupplierVO> list = supplierList.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());
        return list;
    }


}
